"Verbose Dictionary" in C#, 'override new' this[] or implement IDictionary

Posted by Benjol on Stack Overflow See other posts from Stack Overflow or by Benjol
Published on 2010-04-27T05:33:48Z Indexed on 2010/04/27 5:43 UTC
Read the original article Hit count: 321

Filed under:
|
|

All I want is a dictionary which tells me which key it couldn't find, rather than just saying The given key was not present in the dictionary.

I briefly considered doing a subclass with override new this[TKey key], but felt it was a bit hacky, so I've gone with implementing the IDictionary interface, and passing everything through directly to an inner Dictionary, with the only additional logic being in the indexer:

public TValue this[TKey key]
{
    get
    {
        ThrowIfKeyNotFound(key);
        return _dic[key];
    }
    set
    {
        ThrowIfKeyNotFound(key);
        _dic[key] = value;
    }
}
private void ThrowIfKeyNotFound(TKey key)
{
    if(!_dic.ContainsKey(key))
        throw new ArgumentOutOfRangeException("Can't find key [" + key + "] in dictionary");
}

Is this the right/only way to go? Would newing over the this[] really be that bad?

© Stack Overflow or respective owner

Related posts about c#

Related posts about dictionary